from mcp.server.fastmcp import FastMCP, Context
import subprocess
import json
from typing import List

mcp = FastMCP("E2fsprogs MCP Server")

"""
面向 e2fsprogs 软件的 MCP Server

MCP tools 列表：
1. e2fsck_repair - 修复文件系统完整性
   参数：device (str) - 要修复的设备路径
   返回：统一 JSON 结构

2. e2scrub_all_service_status - 检查 e2scrub_all.service 服务状态
   参数：无
   返回：统一 JSON 结构

3. e2scrub_reap_service_status - 检查 e2scrub_reap.service 服务状态
   参数：无
   返回：统一 JSON 结构
"""

@mcp.tool()
def e2fsck_repair(device: str) -> dict:
    """
    使用 e2fsck 修复文件系统完整性
    
    参数:
        device: 要修复的设备路径，如 /dev/loop0
    
    返回:
        {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["e2fsck", "-y", device]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=30)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command timed out"
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def e2scrub_all_service_status() -> dict:
    """
    检查 e2scrub_all.service 服务状态
    
    返回:
        {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["systemctl", "status", "e2scrub_all.service"]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=10)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command timed out"
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

@mcp.tool()
def e2scrub_reap_service_status() -> dict:
    """
    检查 e2scrub_reap.service 服务状态
    
    返回:
        {
            "success": bool,
            "command": str,
            "exit_code": int,
            "stdout": str,
            "stderr": str
        }
    """
    command = ["systemctl", "status", "e2scrub_reap.service"]
    try:
        result = subprocess.run(command, capture_output=True, text=True, timeout=10)
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command timed out"
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": str(e)
        }

if __name__ == "__main__":
    mcp.run()